import {
  seriesOptions,
  BAR_LINE_DEFAULT_OPTION,
  PIE_DEFAULT_OPTION,
} from './options';
export const setOptionFun = (newOption) => {
  let { series } = newOption;
  // console.log('series', series)
  if (typeof series === 'object') {
    if (Array.isArray(series)) {
      series = series.map(m => setSeriesFun(m));
    }
  }
  // console.log('处理完成之后的echarts', series)
  let returnObj = { ...newOption, series };

  if (Array.isArray(series)) {
    let type = series[0].type;
    if (['bar', 'line'].includes(type)) {
      returnObj = deepReplaceFun(BAR_LINE_DEFAULT_OPTION, returnObj);
    }
    if (['pie'].includes(type)) {
      returnObj = deepReplaceFun(PIE_DEFAULT_OPTION, returnObj);
    }
  }
  return returnObj;
};

const setSeriesFun = (obj) => {
  const { type } = obj;
  if (seriesOptions[type]) {
    return deepReplaceFun(seriesOptions[type], obj);
  } else {
    return obj;
  }
};

const deepReplaceFun = (oldObj, newObj) => {
  const sampleFun = (_o, _n) => {
    if (typeof _o !== typeof _n) {
      return _n;
    } else {
      switch (typeof _o) {
        case 'object':
          if (Array.isArray(_o) && Array.isArray(_n)) {
            return (_o.length >= _n.length ? _o : _n).map((k, ki) => {
              if (_o[ki]) {
                return sampleFun(_o[ki], _n[ki]);
              } else {
                return _n[ki];
              }
            });
          } else if (!Array.isArray(_o) && !Array.isArray(_n)) {
            let returnObj = {};
            Object.keys(_n).forEach((m) => {
              if (typeof _o[m] === 'undefined') {
                returnObj[m] = _n[m];
              }
            });
            Object.keys(_o).forEach((m) => {
              if (typeof _n[m] === 'undefined') {
                returnObj[m] = _o[m];
              } else if (
                typeof _o[m] !== 'undefined' &&
                typeof _n[m] !== 'undefined'
              ) {
                returnObj[m] = _n[m];
              } else {
                returnObj[m] = sampleFun(_o[m], _n[m]);
              }
            });
            return returnObj;
          } else {
            return _n;
          }
        default:
          return _n;
      }
    }
  };
  return sampleFun(oldObj, newObj);

};

export const setOptionsFun = (type, options) => {
  let { xData = [], yData = [], ...rest } = options;
  yData = JSON.parse(JSON.stringify(yData));
  let returnObj = {};
  switch (type) {
    case 'bar':
    case 'line':
      returnObj = {
        grid: options.grid || {},
        xAxis: {
          type: 'category',
          data: xData,
          axisLabel: { rotate: 30 },
        },
        series: yData.map((m) => {
          return { ...m, type };
        }),
      };
      break;
    case 'pie':
      returnObj = {
        legend: { show: false },
        series: yData.map((m, mi) => {
          return {
            type: 'pie',
            raduis: '70%',
            center: ['50%', '50%'],
            data: xData.map((n, ni) => {
              return {
                name: n + (m.name ?? ''),
                value: m.data[ni],
              };
            }),
            label: { position: 'outer', alignTo: 'none', blodMargin: 5 },
            left: (100 / yData.length) * mi + '%',
            right: (100 / yData.length) * (yData.length - 1 - mi) + '%',
            top: 0,
            bottom: 0,
          };
        }),
      };
      break;
    default:
      break;
  }
  return { ...returnObj, ...rest };
};
